package com.hongdee.scm.car.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.car.entity.TScmCar;
import com.hongdee.scm.car.service.ITScmCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/5/4.
 */
@RestController
@RequestMapping("/scm/car")
public class TScmCarController {
    @Autowired
    private ITScmCarService itScmCarService;

    /**
     * 整车信息分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = itScmCarService.page(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 车辆列表查询(调拨添加车辆,发票登记添加车辆)
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    @ResponseBody
    public List list(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestBody Map data) throws Exception {

        return itScmCarService.list(uid, data);
    }


    /**
     * 车辆列表查询
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/listCar")
    @ResponseBody
    public List listCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "token", defaultValue = "") String token,
                        @RequestBody Map data) throws Exception {

        return itScmCarService.listCar(uid, data);
    }


    /**
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "0") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TScmCar data) throws Exception {
        TScmCar po = this.itScmCarService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(po);
        return res;
    }

    /**
     * 整车信息详情
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择整车信息！");
        }
        Map map = itScmCarService.info(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 分页查询导入
     * @author 徐钦勇
     */
    @RequestMapping("/impPage")
    @ResponseBody
    public Map impPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit) throws Exception {
        String oid = TokenUtil.getCompanyId(uid);
        Map map = this.itScmCarService.impPage(start, limit, oid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 批量导入确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirms")
    @ResponseBody
    public ResultEntity impConfirms(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody List<Map<Object, String>> id) throws Exception {
        if (org.springframework.util.StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录！");
        }
        List list = this.itScmCarService.impConfirms(id, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("确认成功！");
        res.setData(list);
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 全部导入确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirmAll")
    @ResponseBody
    public ResultEntity impConfirmAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {
        List list = this.itScmCarService.impConfirmAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(list);
        res.setMsg("确认成功！");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入全部删除
     * @author 徐钦勇
     */
    @RequestMapping("/impDelAll")
    @ResponseBody
    public ResultEntity impDelAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.itScmCarService.impDelAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 删除多条待导入的数据
     * @author 徐钦勇
     */
    @RequestMapping("/impDels")
    @ResponseBody
    public ResultEntity impDels(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody List<Map<Object, String>> data) throws Exception {
        if (org.springframework.util.StringUtils.isEmpty(data)) {
            throw new Exception("请选择记录！");
        }
        this.itScmCarService.impDels(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    /**
     * 更新暂估价格(未入库)
     *
     * @param uid
     * @param token
     * @param id
     * @param price
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateEstimate")
    @ResponseBody
    public ResultEntity updateEstimate(@RequestParam(name = "uid", defaultValue = "") String uid,
                                       @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestParam(name = "id", defaultValue = "") String id,
                                       @RequestParam(name = "price", defaultValue = "") String price) throws Exception {
        this.itScmCarService.updateEstimatePprice(id, price, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("更新成功！");
        return res;
    }

    /**
     * 暂时刷新没有车型ID车辆的车型ID
     *
     * @param uid
     * @throws Exception
     */
    @RequestMapping("/flashModelId")
    @ResponseBody
    public ResultEntity flashModelId(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.itScmCarService.flashAllCarModelId(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("车型ID更新成功！");
        return res;
    }

    /**
     * 暂时刷新没有配置ID车辆的配置ID
     *
     * @param uid
     * @throws Exception
     */
    @RequestMapping("/flashConfigIds")
    @ResponseBody
    public ResultEntity flashConfigId(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.itScmCarService.flashAllCarConfigId(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("配置ID更新成功！");
        return res;
    }

    /**
     * 批量删除导入数据错误车辆信息
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/delUserlessCar")
    @ResponseBody
    public ResultEntity delUserlessCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                                       @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestBody String[] ids) throws Exception {
        this.itScmCarService.deleteUserlessCar(ids, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    /**
     * 分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/newPage")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "status", defaultValue = "") String status,
                    @RequestBody Map data) throws Exception {

        Map map = this.itScmCarService.page1(data, status, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 刷新车辆外观内饰名称
     *
     * @param uid
     * @throws Exception
     */
    @RequestMapping("/refershCarAppearanceAndInterior")
    @ResponseBody
    public ResultEntity refershCarAppearanceAndInterior(@RequestParam(name = "uid", defaultValue = "") String uid,
                                                        @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.itScmCarService.refershCarAppearanceAndInterior(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("刷新成功！");
        return res;
    }
    /**
     * 刷新暂估价格
     * @param uid
     * @throws Exception
     */
    @RequestMapping("/refreshEstimatePrice")
    @ResponseBody
    public ResultEntity refreshEstimatePrice(@RequestParam(name = "uid", defaultValue = "") String uid,
                                                        @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.itScmCarService.refreshEstimatePrice(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("刷新成功!");
        return res;
    }

    /**
     * 验车单打印次数
     * @param uid
     * @param token
     * @param vin
     * @return
     * @throws Exception
     */
    @RequestMapping("/printNumber")
    @ResponseBody
    public Map PrintNumber(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token,
                           @RequestBody String[] vin
    )throws Exception{


        //int redAll = 0;
        Map map = new HashMap();

        for(int i=0;i<vin.length;i++){
            this.itScmCarService.addValidateNum(vin[i]);
//            if(i == (vin.length-1)){
//                redAll = 1;
//            }
        }
//
//        if(redAll == 1){
            map.put("success", true);
            map.put("msg", "打印次数+1操作成功");
//
//        }
//        else {
//            map.put("success", false);
//            map.put("msg", "打印次数+1操作失败");
//        }

        return map;
    }

    /**
     * 批量删除导入数据错误车辆信息
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/financeConfirm")
    @ResponseBody
    public ResultEntity financeConfirm(@RequestParam(name = "uid", defaultValue = "") String uid,
                                       @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestBody String[] ids) throws Exception {
        this.itScmCarService.deleteUserlessCar(ids, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    /**
     * 采购收货查询
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/carMIGO")
    @ResponseBody
    public Map carMIGO(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = this.itScmCarService.CarMIGO(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 刷新车系车型配置（2017-10-25）
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/flashConfigId")
    @ResponseBody
    public Map refreshSMC(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {

        Map map = new HashMap();
        this.itScmCarService.refreshCarSMC(uid);
        map.put("success", true);
        map.put("msg", "刷新成功！");
        return map;
    }

    /**
     * 根据vin获取该车辆的信息（针对销售后重新采购回库的车）
     * @param uid
     * @param token
     * @param vin
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryByVin")
    @ResponseBody
    public ResultEntity queryByVin(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "vin", defaultValue = "") String vin
    ) throws Exception {

        ResultEntity entity = new ResultEntity();
        entity.setSuccess(true);
        entity.setMsg("查询成功！");
        List<TScmCar> list = this.itScmCarService.queryByVin(vin,uid);
        entity.setData(list);
        return entity;
    }

}
